package com.ohos.connent.modelcloud.biz.modular.pro.util;

import org.springframework.util.ObjectUtils;

public class SequenceGeneratorUtil {

    private static final String ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static String getNextSequence(String input) {
        if (ObjectUtils.isEmpty(input)) {
            return convertFromDecimal(46657);
        }
        int decimalValue = convertToDecimal(input);
        decimalValue++;
        return convertFromDecimal(decimalValue);
    }

    private static int convertToDecimal(String input) {
        int decimalValue = 0;
        for (int i = 0; i < input.length(); i++) {
            char character = Character.toUpperCase(input.charAt(i));
            int index = ALPHABET.indexOf(character);
            if (index == -1) {
                throw new IllegalArgumentException("Invalid character in input: " + character);
            }
            decimalValue = decimalValue * 36 + index;
        }
        return decimalValue;
    }

    private static String convertFromDecimal(int number) {
        StringBuilder sb = new StringBuilder();
        while (number > 0) {
            sb.append(ALPHABET.charAt(number % 36));
            number /= 36;
        }
        return sb.reverse().toString();
    }
}
